package shapp;

import com.ht.aems.fesmodel.FesServer;
import com.ht.aems.util.RtDataHandler;
import com.ht.dsp.DspFacade;
import com.ht.memcached.MemCachedOpt;
import hhh.remoteService.utils.ServiceQuery;
import org.apache.log4j.Logger;
import rtdb.RtDataHandler_MemCached;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2012-1-12
 */
public class RemoteShApp extends UnicastRemoteObject implements ShApp {
    private static Logger log = Logger.getLogger(RemoteShApp.class);
    NariPointWriter pointWriter = new NariPointWriter();
    public static RemoteShApp instance;
    RestartEasMod rs = new RestartEasMod();
    private RtDataHandler handler = new RtDataHandler_MemCached();

    static {
        try {
            instance = new RemoteShApp();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private RemoteShApp() throws RemoteException {
    }

    public void writeNariYc() throws RemoteException {
        pointWriter.writeYc();
    }

    public void writeNariYx() throws RemoteException {
        pointWriter.writeYx();
    }

    public void restartEas() throws RemoteException {
        log.info("begin restarting the eas mode!!!!");
        try {
            rs.removeJob("eas", "eas1");
            rs.removeJob("eas", "eas2");
            rs.removeJob("eas", "eas3");
            rs.removeJob("eas", "eas4");
            rs.removeMod("eas");

            try {
                Thread.sleep(5000l);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            rs.addMod("/aems/shaems20/eas", "file:///aems/shaems20/eas");
            rs.addJob("eas", "eas1");
            rs.addJob("eas", "eas2");
            rs.addJob("eas", "eas3");
            rs.addJob("eas", "eas4");
        } catch (Exception e) {
            log.warn("restarting eas not successful: " + e.getMessage());
            e.printStackTrace();
        }
        log.info("restarting the eas mode successfull !!!");
    }

    Long lastRestartTime;

    public void easWatcher() throws RemoteException {
        log.info("start check if need restarting the eas mode!!!!");
        Object startTime = handler.get("Key_OpfStartTime");
        Object endTime = handler.get("Key_OpfEndTime");
        if (startTime != null && endTime != null) {
            long costTime = (Long) endTime - (Long) startTime;
            if (costTime > 10000l) {
                if (lastRestartTime == null)
                    log.info("lastRestartTime is null...");
                else
                    log.info("lastRestartTime is " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(lastRestartTime)));
                if (lastRestartTime != null && ((Long) endTime - lastRestartTime) < 20 * 60000l) {
                    return;
                }
                log.info("begin restarting the eas mode!!!!");
                try {
                    rs.removeJob("eas", "eas1");
                    rs.removeJob("eas", "eas2");
                    rs.removeJob("eas", "eas3");
                    rs.removeJob("eas", "eas4");
                    rs.removeMod("eas");

                    try {
                        Thread.sleep(5000l);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    rs.addMod("/aems/shaems20/eas", "file:///aems/shaems20/eas");
                    rs.addJob("eas", "eas1");
                    rs.addJob("eas", "eas2");
                    rs.addJob("eas", "eas3");
                    rs.addJob("eas", "eas4");
                } catch (Exception e) {
                    log.warn("restarting eas not successful: " + e.getMessage());
                    e.printStackTrace();
                }
                lastRestartTime = System.currentTimeMillis();
                log.info("restarting the eas mode successfull !!!");
            }
        } else {
            log.info("eas model watcher failed, can't get eas' opf timestamp");
        }
    }

   

    public NariPointWriter getPointWriter() {
        return pointWriter;
    }

    public void ucpWatcher() {
        log.info("start check if need restarting the ucp mode!!!!");
        Object t1Value = MemCachedOpt.get(FesServer.T1ERROR);
        if(t1Value == null)
            return;
        boolean isNeedRestart = Integer.parseInt(t1Value.toString()) == 0;
        if (isNeedRestart) {
                log.info("begin restarting the ucp mode!!!!");
                try {
                    rs.removeJob("ucp", "ucp");
                    rs.removeMod("ucp");
                    try {
                        Thread.sleep(5000l);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    rs.addMod("/aems/shaems20/ucp", "file:///aems/shaems20/ucp");
                    rs.addJob("ucp", "ucp");
                } catch (Exception e) {
                    log.warn("restarting ucp not successful: " + e.getMessage());
                    e.printStackTrace();
                }
                MemCachedOpt.set(FesServer.T1ERROR, 1);
                log.info("restarting the ucp mode successfull !!!");
            DspFacade dspService = (DspFacade) ServiceQuery.getService(DspFacade.class);
            if (dspService != null) {
                try{
                    Thread.sleep(5 * 60000l);
                    dspService.fill104Data();
                } catch (Exception e) {
                    e.printStackTrace();
                    log.warn("fill 104 points failed!");
                }
            }
        } else {
            log.info("not need to restart ucp");
        }
    }
}
